package com.lalala.kafkaproducer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.kafka.core.KafkaTemplate;

import javax.annotation.PostConstruct;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;

@SpringBootApplication
public class KafkaProducerApplication {

    @Autowired
    private  KafkaTemplate kafkaTemplate;

    @Autowired
    private  static KafkaTemplate kafkaTemplatestatic;

    @Value("${staffkafka.topicsId}")
    private String topic;

    private static  String Topic;

    @PostConstruct
    public void init() {
        kafkaTemplatestatic = kafkaTemplate;
        Topic = topic;
    }
    public static void main(String[] args) {
        SpringApplication.run(KafkaProducerApplication.class, args);
        send();
    }

    public static void send() {
        new Timer().schedule(new TimerTask() {
            long number=1;
            @Override
            public void run() {
                UUID uuid = UUID.randomUUID();
                System.out.println("第"+number+"次生成UUID,生成的UUID为----->"+uuid.toString());
                kafkaTemplatestatic.send(Topic, "第"+number+"次生成UUID,生成的UUID为----->"+uuid.toString());
                number++;
            }
        },5000, 10000);
    }
}
